<?php
/*
 * emarket - a website to create your own online-shop.  
 * Copyright (C) 2009  Frédéric Delorme
 *
 * This file is part of emarket.
 *
 * emarket is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * emarket is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with emarket.  If not, see <http://www.gnu.org/licenses/>.
 *
 * To contact me: frederic.delorme@gmail.com
 * to see some information on te project : http://code.google.col/p/os-emarket/
 *
 */
/**
 * Autoloader experiment to avoid "<pre>require_once</pre>" in all files.
 * @author frederic 
 * @deprecated
 */
require_once("framework/initialize.php");

function __autoload($class_name){
		global $framework;
		$fmkclasses=array(
		'Ajax'=>"$framework/",
		'Configuration'=>"$framework/",
		'DataFile'=>"$framework/",
		'DataMysql'=>"$framework/",
		'Debug'=>"$framework/",
		'Fields'=>"$framework/",
		'Forms'=>"$framework/",
		'Html'=>"$framework/",
		'I18n'=>"$framework/",
		'IDAta'=>"$framework/",
		'Image'=>"$framework/",
		'Model'=>"$framework/",
		'Paginator'=>"$framework/",
		'Router'=>"$framework/",
		'Session'=>"$framework/",
		'Template'=>"$framework/",
		'WebApp'=>"$framework/",
		'EntityNotFoundException'=>"$framework/exception",
		'ConfigurationHelpers'=>"$framework/helpers",
		'DebugHelpers'=>"$framework/helpers",
		'I18nHelpers'=>"$framework/helpers",
		'ImageHelpers'=>"$framework/helpers",
		'TemplateHelpers'=>"$framework/helpers",
		'WebAppHelpers'=>"$framework/helpers",
		);
		$helpers_dir = "helpers/";
		$app_dir=array(
			"webapp/application/",
			"webapp/managers/",
			"webapp/helpers/",
			"webapp/entities/",
		);
		//class directories
		$fmk_dir = array(
			"$framework/",
			"$framework/exception/",
			"$framework/patterns/",
			"$framework/tests/",
		);
		
		
		//echo "<p>Request :</p><pre>".print_r($_SERVER['REQUEST_URI'],true)."</pre>";
		//echo "<p>Search for $class_name</p><ul>";
		$lname = strtolower($class_name);
		if(strpos($lname,"helpers")>0){
			$pos = strpos($lname,"helpers");
			$name=substr($lname,0,$pos);
			$lname=$name.".helpers";
			//echo "<li>it's a helpers class!</li>";
		}
		//Specify Directory where to search.
		if(isset($fmkclasses[$class_name])){
			$directories=$fmkclasses[$class_name]."/";
			//echo "<li>it's a framework class : dir='$directories'.</li>";

		}else if(strpos($lname,"helpers")>0){
			$directories = $helpers_dir;
		
		}else{ 
			$directories = $app_dir;
		
		}
	
		//for each directory
		if(isset($directories) && is_array($directories)){
			foreach($directories as $directory){
				testPath($directory,$lname);			
				return;
			}
		}else{
			testPath($directories,$lname);
			return;			
		}
		//echo "</ul>";
	}

	function testPath($directory,$lname){
		$filepath = $_SERVER['DOCUMENT_ROOT'].$directory.$lname . '.php';
		//echo "<li>try to load $lname as $filepath</li>";
		//see if the file exsists
		if(file_exists($filepath)){
			//echo "<li>found !</li>";
			require_once($filepath);
			//only require the class once, so quit after to save effort (if you got more, then name them something else
			return;
		}		   
	}
	?>